package com.lhz.collector.controller;

import com.lhz.collector.common.RestfulResult;
import com.lhz.collector.entity.User;
import com.lhz.collector.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by LHZ on 2017/3/29.
 */
@Controller
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public ResponseEntity register(@RequestParam String username, @RequestParam String password){
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        RestfulResult restfulResult=userService.register(user);
        return ResponseEntity.ok(restfulResult);
    }

    @RequestMapping(value = "/changePassword",method = RequestMethod.POST)
    public ResponseEntity changePassword(Authentication authentication,@RequestParam String oldPassword,@RequestParam String newPassword){
        String username=authentication.getName();
        RestfulResult restfulResult=userService.changePassword(username,oldPassword,newPassword);
        return ResponseEntity.ok(restfulResult);
    }
}